
--练习字符串操作函数

str = "Hello world Hello"

print("获取长度",string.len(str))
print("大小写转换",string.lower(str))
print("大小写转换",string.upper(str))
print("截取字符串",string.sub(str,3,5)) -- 截取下标从 3到5的字符串 包括5 返回新的字符串
print("字符串替换",string.gsub(str,'Hello','Hi',1)) --把字符串中的Hello替换成Hi 返回新的字符串和替换次数 参数 1 表示只替换1次 不传则全部替换

print("获取ascii码",string.byte("A",1))
print("ascii码转换成字符串",string.char(97))
print("格式化输出",string.format("要输出浮点数%.4f",math.pi))

startIdx,endIdx = string.find(str,"Hello")
print("查找字符串",startIdx,endIdx)

--http://blog.csdn.net/zhangxaochen/article/details/8095007
math.randomseed(tostring(os.time()):reverse():sub(1, 6)) --随机数设置随机种子很重要

print("--12",math.random(1,100))

repNum = 2
print("字符串替换",string.gsub("这是一串，cute very cute 哈哈 cute ",'cute','great',repNum)) --返回替换后的字符串和替换次数

s = "sin(3) = $[math.sin(3)]; 2^5 = $[2^5]"
for w in string.gfind(s, "%a+") do -- gfind 返回的是迭代器
 
	print("gfind",w)
end

-- 2017年9月7日11:54:48 154

function string.split(src,split)
	ary = {}
	stIdx = 1		
	i,j = string.find(src,split,stIdx)
	while i do
		s = string.sub(src,stIdx,i-1)
		table.insert(ary,s)
		stIdx = j+1
		i,j = string.find(src,split,stIdx)
	end 	
	s = string.sub(src,stIdx)
	table.insert(ary,s)
	return ary
end

function table.join(srcTab,split)
	str = ""
	for k,v in pairs(srcTab) do
		str = str .. v ..split
	end
	str = string.sub(str,1,string.len(str)-1)
	return str
end

function ChatStrToArr(s,delimParttern)
    if type(delimParttern) ~= "string" or string.len(delimParttern) <= 0 then
        return {};
    end
    local start = 1
    local t = {}
    while true do
        local pos,endPos,value = string.find (s, delimParttern, start) 
        if not pos then
            break
        end
        local tempStr = string.sub (s, start, pos-1 )
        if tempStr~="" and tempStr~=nil then
            table.insert (t, tempStr)
        end
        if value~="" and value~=nil then
            table.insert (t, value)
        end

        start = pos + string.len(value)
    end
    local temp = string.sub (s, start)
    if temp~="" and temp~=nil then
        table.insert (t, temp)
    end
    return t;
end

awardStr = "10,20,30"
--print(string.find(awardStr,",",3))
ary = string.split(awardStr,",")

for k,v in pairs(ary) do
	print(k,v)
end

print(table.join(ary,";"))

str = "var name:string = 'zhou' /** 这是注释 **/ 宏37games"

i,j = string.find(str,"/%*%*.*%*%*/")
print(i,j)
print(string.sub(str,i,j)) --2017年9月8日19:19:17 171

local html = "          空格结束<color=#584638>追日夺星 </color>自身全属性提高15%;上阵神灵命中率+12%  <color=#dc4300>(6阶解锁)</color>"

i,j = string.find(html,"<color=.->")
print("html",i,j)
local colorParttern = "(<color=#[%a%d]+>.-</color>)";
local temps = ChatStrToArr(html,colorParttern)
for i, v in ipairs(temps) do
	print("ChatStrToArr v=",v)
end
local name = "	空白	名字	"
--name = "你好233哈哈89风动旛动"
local re = string.gmatch( name,"%d+" )
local stIdx,endIdx = string.find(name,"%s")
print("re=",re,"stIdx,endIdx=",stIdx,endIdx)
for v in re do
    print("v=",v)
end

local base = 200
local total = 0
for i = 1, 12, 1 do
    total = total + base
    base = base + 50
end

local linkStr = "例如：公告内容<a href=https://www.baidu.com/>百度首页</a>公告内容,哈哈<a href=https://www.mike.com/>米壳游戏</a>，啦啦啦<a href=https://www.youxi.com/>游戏</a>"
--local match = string.match(linkStr,"<a href=.->(.-)</a>",1)
local parttern = "<a href=(.-)/>(.-)(</a>)" --正则模式定义了2个组
local matchs = string.gmatch(linkStr,parttern) --只能取出最前面组别的内容
for m in matchs do
    print("m=",m)
end

local linkInfos = {}
local idx = 1
local stIdx,endIdx,link,name,a = string.find(linkStr,parttern,idx) --匹配多个组 要用find函数
print("find 1=",stIdx,endIdx,link,name,a)
while stIdx do
    table.insert(linkInfos,{link=link,name=name})
    idx = endIdx
    stIdx,endIdx,link,name = string.find(linkStr,parttern,idx)
    print("find 2=",stIdx,endIdx,link,name)
end
if link and name then
    table.insert(linkInfos,{link=link,name=name})
end
